from views import page as pageBase

from api import api


class ResPartner(object):
    page = pageBase.resPartner

    def test(self,):
        self.test_fg()
        self.test_onchange2()
        # self.test_create()
        # print('sech ok,', self.page)

        # self.test_search()

        # self.test_search()
        # self.test_browse()
        # self.test_write()
        # self.test_unlink()

        # res = self.odoorpc.search([])
        # print(res)
        # res = self.read(1, ['name'])
        # print(res)
        # res = self.odoorpc.read(1, ['name'])
        # print(res)

        # self.test_export()
        # self.test_import()

    def test_onchange2(self,):
        field_onchange = {
            "property_payment_term_id": "",
            "property_supplier_payment_term_id": "",
            "property_account_position_id": "",
            "company_id": "1",
            "property_account_receivable_id": "",
            "property_account_payable_id": "",

            "parent_id": "1",
            "name": "1",
            "active": "1",
            "type": "1",
            "is_company": "1",
            "company_type": "1",
            "lang": "1",
            "category_id": "",
            "customer_rank": "",
            "supplier_rank": "",
            "property_stock_customer": ""
        }

        context = {'allowed_company_ids': [1], 'default_company_id': 1}

        res = api.odooenv['res.partner'].onchange2(
            [], {}, [], field_onchange, context=context)

        print('--value-')
        for r, v in res['value'].items():
            print(r, v)
        print('--domain-')
        for r, v in res['domain'].items():
            print(r, v)
        print('--fields-')
        for r, v in res['fields'].items():
            print(r, v)

    def test_env(self,):
        obj1 = api.env['res.partner']
        obj2 = api.odooenv['res.partner']

        # res = obj.browse_one([1])
        print('1', obj1.env)

        print('2', obj2.env)

        res = obj2.env['product.product'].read2([1])
        print(res)

    def test_fg(self,):
        # res = api.odooenv.ref('model_res_partner')
        # res = api.odooenv.ref2('base.model_res_partner')
        # print(res)

        # session = api.session
        # current_company = session['user_companies']['current_company']
        # print(current_company)
        # allowed_companies = session['user_companies']['allowed_companies']
        # print(allowed_companies)

        # res = api.env['res.partner'].fields_get2(
        #     [], ['type', 'domain'], fields_type='test')

        # res = api.env['res.partner'].search_read(
        #     "[('company_id', '!=', 1)]", ['name'])
        # print('- ptn--', res)

        self.test_fg_model('res.partner')
        self.test_fg_model('res.partner', 2)
        self.test_fg_model('product.template')
        self.test_fg_model('product.template', 2)
        self.test_fg_model('account.move')
        self.test_fg_model('account.move', 2)
        self.test_fg_model('account.move.line')
        self.test_fg_model('account.move.line', 2)
        self.test_fg_model('sale.order')
        self.test_fg_model('sale.order', 2)
        self.test_fg_model('sale.order.line')
        self.test_fg_model('sale.order.line', 2)
        self.test_fg_model('purchase.order')
        self.test_fg_model('purchase.order', 2)
        self.test_fg_model('purchase.order.line')
        self.test_fg_model('purchase.order.line', 2)
        self.test_fg_model('account.payment')
        self.test_fg_model('account.payment', 2)
        self.test_fg_model('stock.picking')
        self.test_fg_model('stock.picking', 2)
        self.test_fg_model('stock.move')
        self.test_fg_model('stock.move', 2)

    def test_fg_model(self, model, ver=None):
        print('---', model, ver)
        context = {'allowed_company_ids': [1]}
        globals_dict = {'company_id': 1}

        if ver:
            res = api.odooenv[model].fields_get2(
                [], ['type', 'domain'], globals_dict=globals_dict, context=context)
        else:
            res = api.odooenv[model].fields_get(
                [], ['type', 'domain'], context=context)

        for k, r in res.items():
            if r['type'] == 'many2one' and r['domain']:
                print(type(r['domain']),  k,   r['domain'])

    def test_search(self):
        domain = [('name', 'like', 'www')]
        # domain = []
        self.page.showList(domain)
        print('sech ok --------,', self.page._name)
        print('sech ok --------,', self.page.store.dataLength)
        for r in self.page.dataList:
            print('sech ok --for------,', r)
        # return self.page.store.dataList

    def test_browse(self):
        rid = self.page.dataList[0]['id']
        # rid = 56
        print('--- in test, test_browse rid-1--', rid)
        dataDict = self.page.dataDict
        print('--- in test, test_browse dataDict-1--', dataDict)
        self.page.showView(rid)
        dataDict = self.page.dataDict
        print('--- in test, test_browse dataDict-2--', dataDict)

    def test_unlink(self):
        print('--- in test, test_unlink dataDict-1--', )
        rid = self.page.store.dataDict['id']
        print('--- in test, test_unlink dataDict-2--', rid)
        self.page.handelDel(rid)

    def test_write(self):
        dataDict = self.page.store.dataDict
        formData = self.page.store.formData
        values = self.page.store.values
        print('--- in test, test_write-1-dataDict-', dataDict)
        print('--- in test, test_write-1-formData-', formData)
        print('--- in test, test_write--1-values:', values)
        rid = self.page.store.dataDict['id']
        self.page.showForm(rid)
        dataDict = self.page.store.dataDict
        formData = self.page.store.formData
        values = self.page.store.values
        print('--- in test, test_write-2-dataDict-', dataDict)
        print('--- in test, test_write-2-formData-', formData)
        print('--- in test, test_write-2-values:', values)

        input_value = ['name', 'www222']
        self.page.handleOnchange(*input_value)
        dataDict = self.page.store.dataDict
        formData = self.page.store.formData
        values = self.page.store.values
        print('--- in test, test_write-3-dataDict-', dataDict)
        print('--- in test, test_write-3-formData-', formData)
        print('--- in test, test_write-3-values:', values)

        self.page.submitWrite(rid)
        dataDict = self.page.store.dataDict
        print('--- in test, dataDict--4-', dataDict)

    def test_create(self):
        self.page.showForm()

        # fieldsGet = self.page.store.fieldsGet
        # print('  --- in test, fieldsGet-----', )
        # for k, v in fieldsGet.items():
        #     if v.get('type') == 'selection':
        #         print('  --%s, type:%s, options:%s' %
        #               (k, v.get('type'), v.get('options')))
        #     if v.get('type') in ['many2one', 'many2many']:
        #         print('  --%s, type:%s, options:%s, relation:%s, domain:%s'
        #               % (k, v.get('type'), v.get('options'),  v.get('relation'), v.get('domain'),))

        dataDict = self.page.store.dataDict
        formData = self.page.store.formData
        values = self.page.store.values
        print('--- in test, dataDict-1--', dataDict)
        print('--- in test, formData--1-', formData)
        print('--- in test, values--1-', values)

        input_value = ['name', 'www122']
        self.page.handleOnchange(*input_value)
        input_value = ['is_company', False]
        self.page.handleOnchange(*input_value)
        input_value = ['company_type', "company"]
        self.page.handleOnchange(*input_value)

        # # company_id_meta = fieldsGet['company_id']
        # # company_id_options = company_id_meta['options']
        # # print(company_id_options)

        # # input_value = ['company_id', 2]
        # # self.page.handleOnchange(*input_value, context=context)

        dataDict = self.page.store.dataDict
        formData = self.page.store.formData
        values = self.page.store.values
        print('--- in test, dataDict--2-', dataDict)
        print('--- in test, formData--2-', formData)
        print('--- in test, values--2-', values)

        self.page.submitCreate()
        dataDict = self.page.store.dataDict
        print('--- in test, dataDict--3-', dataDict)

    def test_export(self):
        ids = []
        domain = [('name', 'like', 'w')]

        res = self.page.export_file(ids, domain)

        filename = res['filename']
        with open('data/' + filename, "wb") as fp:
            fp.write(res['data'])

        res = self.page.export_file(ids, domain, 'xlsx')
        filename = res['filename']
        with open('data/' + filename, "wb") as fp:
            fp.write(res['data'])

    def test_export_by_base64(self):
        ids = self.search([('name', '=', 'w23456')])
        file_extension = 'xlsx'
        # file_extension = 'csv'
        res = self.export_base64(ids, file_extension=file_extension)
        # print('res,', res)

        base64str = res['base64str']
        # extension = res['extension']
        file_name = 'data/' + res['file_name']
        self.js_write_file(file_name, base64str)

    def test_import_by_base64(self):
        file_name = 'res_partner.csv'
        path = 'data/' + file_name

        file_base64 = self.js_read_file_base64(path)
        file_type = self.get_content_type(file_name)

        res = self.import_base64(file_name, file_base64, file_type)

        print(res)

    def test_import(self):
        file_name = 'res_partner.csv'
        file_name = 'res_partner.xls'
        file_name = 'res_partner.xlsx'

        path = 'data/' + file_name

        res = self.page.test_import_file(path)

        print('------1------res,,')
        print(res)


class ResPartnerCompany(ResPartner):
    page = pageBase.resPartnerCompany
    pass


class ResPartnerCompanyCustomer(ResPartnerCompany):
    page = pageBase.resPartnerCompanyCustomer
    pass


resPartner = ResPartner()
resPartnerCompany = ResPartnerCompany()
resPartnerCompanyCustomer = ResPartnerCompanyCustomer()
